/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package br.terminalrodoviario.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import br.terminalrodoviario.City;

/**
 *
 * @author TIAGO
 */
public class CityServlet extends HttpServlet {
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            String action = request.getParameter("acao");
            String id = request.getParameter("id");
            if(request.getParameter("delete.x") != null || request.getParameter("delete.y") != null)
                delete(request, response);
            else if((request.getParameter("edit.x") != null || request.getParameter("edit.y") != null) && id != null)
                response.sendRedirect("Fields/FCity.jsp?id="+id);
            else if(action.equalsIgnoreCase("gravar") && id.equals("null"))
                insert(request, response);
            else if(action.equalsIgnoreCase("gravar") && !id.equals("null"))
                update(request, response);
        } finally { 
            out.close();
        }
    }

    private void insert(HttpServletRequest request, HttpServletResponse response) throws IOException{
        City c = fillObject(request);
        c.insert();
        try{
            response.sendRedirect("Manager/MCity.jsp");
        }catch(IOException ex){

        }
    }

    private City fillObject(HttpServletRequest request){
        City c = new City();
        c.setName(request.getParameter("txtName"));
        c.setState(request.getParameter("cboState"));
        return c;
    }

    private void delete(HttpServletRequest request, HttpServletResponse response){
        String[] ids = request.getParameterValues("id");
        if (ids != null){
              for(int k = 0; k< ids.length; k++){
                City c = new City();        
                    c.setId(Integer.parseInt(ids[k]));
                    c.delete();
              }
        }

        try{
            response.sendRedirect("Manager/MCity.jsp");
        }catch(IOException ex){
        
        }
    }

    private void update(HttpServletRequest request, HttpServletResponse response){
        City c = fillObject(request);
        String id = request.getParameter("id");
        c.setId(Integer.parseInt(id));
        c.update();
        try{
            response.sendRedirect("Manager/MCity.jsp");
        }catch(IOException ex){

        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
